#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <sys/time.h>
#include <boost/algorithm/string.hpp>
#include "cppjieba/Jieba.hpp"

namespace ns_util
{
    class FileUtil
    {
    public:
        static bool ReadFile(const std::string &file_path, std::string *out)
        {
            std::ifstream in(file_path, std::ios::in);
            if (!in.is_open())
            {
                std::cerr << "open " << file_path << " error" << std::endl;
                return false;
            }
            (*out).clear();

            std::string line;
            while (getline(in, line))
            {
                *out += line;
            }
            in.close();
            return true;
        }
    };
    class StringUtil
    {
    public:
        static void CutString(const std::string &line, std::vector<std::string> *results, const std::string &sep)
        {
            boost::split(*results, line, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };

    const char *const DICT_PATH = "/third_part/cppjieba/dict/jieba.dict.utf8";
    const char *const HMM_PATH = "/third_part/cppjieba/dict/hmm_model.utf8";
    const char *const USER_DICT_PATH = "/third_part/cppjieba/dict/user.dict.utf8";
    const char *const IDF_PATH = "/third_part/cppjieba/dict/idf.utf8";
    const char *const STOP_WORD_PATH = "/third_part/cppjieba/dict/stop_words.utf8";

    class JiebaUtil
    {
    private:
        static cppjieba::Jieba jieba;

    public:
        static void CutString(const std::string &src, std::vector<std::string> *out)
        {
            jieba.CutForSearch(src, *out);
        }
    };
    cppjieba::Jieba JiebaUtil::jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);


    class TimeUtil
    {
        public:
        static std::string GetTimeStamp()
        {
            // // 1、用timeofday但是不能体现出年月日的区别
            // struct timeval _time;
            // gettimeofday(&_time,nullptr);
            // return std::to_string(_time.tv_sec);

            //2、第二种
            // 1. 获取时间戳
            time_t currtime=time(nullptr);

            // 2. 如何把时间戳转换成为20XX-08-04 12:27:03
            struct tm currtm;
            localtime_r(&currtime,&currtm);

             // 3. 转换成为字符串 -- dubug?
             char timebuffer[1024];
             snprintf(timebuffer,sizeof(timebuffer),"%4d-%02d-%02d %02d:%02d:%02d",
                currtm.tm_year+1900,
                currtm.tm_mon+1,
                currtm.tm_mday,
                currtm.tm_hour,
                currtm.tm_min,
                currtm.tm_sec
                );
                 return timebuffer;
        }

        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time,nullptr);
            return std::to_string(_time.tv_sec*1000+_time.tv_usec/1000);
        }
    };
};